/*
 * @(#) DateHiddenTag.java  1.0  2004-4-15
 *
 * Copyright 2003 by LongTop Corporation.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * LongTop Corporation ("Confidential Information").  You
 * shall not disclose such Confidential Information and shall use
 * it only in accordance with the terms of the license agreement
 * you entered into with LongTop.
 */

package com.cnc.tag;

import com.cnc.bas.util.LTFormatUtil;

import org.apache.struts.taglib.html.HiddenTag;
import org.apache.struts.util.RequestUtils;
import org.apache.struts.util.ResponseUtils;

import javax.servlet.jsp.JspException;

/**
 * User: Du bf
 * Date: 2005-3-25
 * Time: 17:55:54
 */
public class DateHiddenTag extends HiddenTag {
    private String datetype;
    public String pattern;
    private final String defaultDatePattern = "yyyy-MM-dd";
    private final String defaultDateTimePattern = "yyyy-MM-dd HH:mm:ss";

    public String getPattern() {
        return pattern;
    }

    public void setPattern(String pattern) {
        this.pattern = pattern;
    }

    public String getDatetype() {
        return datetype;
    }

    public void setDatetype(String datetype) {
        this.datetype = datetype;
    }

    /**
     * Generate the required input tag, followed by the optional rendered text.
     * Support for <code>write</code> property since Struts 1.1.
     *
     * @throws JspException if a JSP exception has occurred
     */
    public int doStartTag() throws JspException {

        // Render the <html:input type="hidden"> tag as before
        // super.doStartTag();

        this.pattern = (this.pattern == null || this.pattern.equals("")) ? defaultDatePattern : this.pattern;
        if (this.datetype != null) {
            if (this.datetype.equalsIgnoreCase("date"))
                this.pattern = defaultDatePattern;
            else if (this.datetype.equalsIgnoreCase("datetime"))
                this.pattern = defaultDateTimePattern;
        }
        StringBuffer results = new StringBuffer("<input type=\"");
        results.append(type);
        results.append("\" name=\"");
        // * @since Struts 1.1
        if (indexed)
            prepareIndex(results, name);
        results.append(property);
        results.append("\"");
        if (accesskey != null) {
            results.append(" accesskey=\"");
            results.append(accesskey);
            results.append("\"");
        }
        if (accept != null) {
            results.append(" accept=\"");
            results.append(accept);
            results.append("\"");
        }
        if (maxlength != null) {
            results.append(" maxlength=\"");
            results.append(maxlength);
            results.append("\"");
        }
        if (cols != null) {
            results.append(" size=\"");
            results.append(cols);
            results.append("\"");
        }
        if (tabindex != null) {
            results.append(" tabindex=\"");
            results.append(tabindex);
            results.append("\"");
        }
        results.append(" value=\"");
        Object oValue = RequestUtils.lookup(pageContext, name, property, null);
        if (oValue != null) {
            if ((this.pattern == null) || (this.pattern.equals("")) || (this.pattern.equalsIgnoreCase("yyyy-mm-dd")))
                value = LTFormatUtil.formatDate(oValue);
            else
                value = LTFormatUtil.formatDateTime(oValue);
        } else
            value = "";
        results.append(ResponseUtils.filter(value));
        results.append("\"");
        results.append(prepareEventHandlers());
        results.append(prepareStyles());
        results.append(getElementClose());
        ResponseUtils.write(pageContext, results.toString());
        // Is rendering the value separately requested?
        if (!write) {
            return (EVAL_BODY_BUFFERED);
        }

        return (EVAL_BODY_BUFFERED);
    }
}

